package com.company.myfirstjmixproject.entity.bom;

import com.company.myfirstjmixproject.entity.enums.BomTypeEnum2;
import com.company.myfirstjmixproject.entity.enums.ByProductEnum;
import com.company.myfirstjmixproject.entity.enums.StateEnum;
import io.jmix.core.entity.annotation.JmixGeneratedValue;
import io.jmix.core.metamodel.annotation.Composition;
import io.jmix.core.metamodel.annotation.InstanceName;
import io.jmix.core.metamodel.annotation.JmixEntity;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@JmixEntity
@Table(name = "MST_BOM_HEADER", indexes = {
        @Index(name = "IDX_MST_BOM_HEADER_SITE", columnList = "SITE_CODE")
})
@Entity(name = "bom_header")
public class BomHeader {
    @JmixGeneratedValue
    @Column(name = "ID", nullable = false)
    @Id
    private UUID id;

    @Column(name = "VERSION", nullable = false)
    @Version
    private Integer version;

    @Column(name = "BOM_CODE", nullable = false)
    private String bomCode;

    /**
     * 创建的实体，可以在messages_en.properties本地化消息配置文件中进行配置绑定
     */
    @InstanceName
    @Column(name = "BOM_NAME", nullable = false)
    private String bomName;


//    @Column(name = "BOM_TYPE", nullable = false)
//    private String bomType;


    @Column(name = "BOM_TYPE", nullable = false)
    @NotNull
    private String bomType;

    /**
     * 在JMIX中，一个字段的属性类型为ENUM，是通过GET/SET方法来体现的，set方法参数为Enum,get方法返回值为Enum
     * 注意⚠️：但是有个问题，被使用的Enum需要实现EnumClass，好像才可以？？？？，因为我没有实现EnumClass的情况下是报错的
     * <p>
     * 在liquibase脚本中怎么体现了，后续查看
     * <p>
     * //     * @param bomType
     */
//    public void setBomType(BomTypeEnum bomType) {
//        this.bomType = bomType == null ? null : bomType.getType();
//    }
//
//    public BomTypeEnum getBomType() {
//        return bomType == null ? null : BomTypeEnum.fromType(bomType);
//    }

    @JoinColumn(name = "SITE_CODE", nullable = false)
    @NotNull
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Site site;

    @JoinColumn(name = "ITEM_CODE", nullable = false)
    @NotNull
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Item item;

    @Column(name = "BY_PRODUCT", nullable = false)
    private String byProduct;

    @Column(name = "BOM_STATE", nullable = false)
    private String bomState;

    @OrderBy("scrapRatio")
    @Composition
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "bomHeader")
    private List<BomDetail> bomDetails;

    @Column(name = "CREATE_BY", nullable = false)
    private String createBy;

    @Column(name = "CREATE_TIME")
    private LocalDateTime createTime;

    @Column(name = "LAST_MODIFIED_BY")
    private String lastModifiedBy;

    @Column(name = "LAST_MODIFIED_TIME")
    private LocalDateTime lastModifiedTime;

    public void setBomDetails(List<BomDetail> bomDetails) {
        this.bomDetails = bomDetails;
    }

    public List<BomDetail> getBomDetails() {
        return bomDetails;
    }

    public void setLastModifiedTime(LocalDateTime lastModifiedTime) {
        this.lastModifiedTime = lastModifiedTime;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public String getCreateBy() {
        return createBy;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public LocalDateTime getLastModifiedTime() {
        return lastModifiedTime;
    }

    public StateEnum getBomState() {
        return bomState == null ? null : StateEnum.fromId(bomState);
    }

    public void setBomState(StateEnum bomState) {
        this.bomState = bomState == null ? null : bomState.getId();
    }

    public ByProductEnum getByProduct() {
        return byProduct == null ? null : ByProductEnum.fromId(byProduct);
    }

    public void setByProduct(ByProductEnum byProduct) {
        this.byProduct = byProduct == null ? null : byProduct.getId();
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public Item getItem() {
        return item;
    }


    public BomTypeEnum2 getBomType() {
        return bomType == null ? null : BomTypeEnum2.fromId(bomType);
    }

    public void setBomType(BomTypeEnum2 bomType) {
        this.bomType = bomType == null ? null : bomType.getId();
    }

    public Site getSite() {
        return site;
    }

    public void setSite(Site site) {
        this.site = site;
    }

    public void setBomName(String bomName) {
        this.bomName = bomName;
    }

    public String getBomName() {
        return bomName;
    }

    public void setBomCode(String bomCode) {
        this.bomCode = bomCode;
    }

    public String getBomCode() {
        return bomCode;
    }


    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }
}